﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IRIS.Models;

namespace IRIS.Controllers
{
    [HandleError]
    public class SponsorController : Controller
    {
        //
        // GET: /Sponsor/
        DataRepo m_db = new DataRepo();

        [Authorize(Roles = "Stjórnendur")]
        public ActionResult Index()
        {
            var model = m_db.getSponsors();
            return View(model);
        }

        #region Create

        public ActionResult Create()
        {
            return View();
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Create(Sponsor theFormData)
        {
            if (string.IsNullOrEmpty(theFormData.Name))
                ModelState.AddModelError("Name", "Nafn má ekki vanta");
            if (string.IsNullOrEmpty(theFormData.URL))
                ModelState.AddModelError("URL", "Heimasíðu má ekki vanta");
            try
            {
                if (ModelState.IsValid)
                { 
                theFormData.Alive = true;
                m_db.addSponsor(theFormData);
                return RedirectToAction("Index");
                }
                    //If not valid return model state
                    return View(theFormData);
            }
            catch
            {
                return View("Error");
            }
         }
        #endregion
        //Returns the Sponsor PartialView to the MasterLayout.
        [ChildActionOnly]
        public ActionResult _SponsorsMenu()
        {
            var model = m_db.getSponsors().Take(3); //Todo: Should we display more?
            return PartialView(model);
        }
        #region Edit
         //Get handles gives a view from the data repository to the view
        [Authorize(Roles = "Stjórnendur")]
        public ActionResult Edit(int? id, Boolean delete)
        {
           if(id.HasValue)
           {
               //Check if delete is true and mark sponsor dead
               if (delete)
               {
                   var model = m_db.getSponsorById(id.Value);
                   model.Alive = false;
                   m_db.editSponsor(id.Value,model);
                   return RedirectToAction("Index");
               }
               return View(m_db.getSponsorById(id.Value));
           }
            return View("NotFound");
        }
        // Post handles the data we get back from the view and returns it to the data repo
        [Authorize(Roles = "Stjórnendur")]
        [HttpPost] 
        public ActionResult Edit(int? id, Sponsor theFormData)
        {
            if (id.HasValue)
            {
                if (string.IsNullOrEmpty(theFormData.Name))
                    ModelState.AddModelError("Name", "Nafn má ekki vanta");
                if (string.IsNullOrEmpty(theFormData.URL))
                    ModelState.AddModelError("URL", "Heimasíðu má ekki vanta");
                try
                {
                    if (ModelState.IsValid)
                    {
                        theFormData.Alive = true;
                        m_db.editSponsor(id.Value, theFormData);
                        return RedirectToAction("Index");
                    }
                    return View(theFormData);
                }
                catch
                {
                    return View("Error");
                }
            }
            return View("NotFound");
        }
        #endregion

    }
}
